//
//  RecommendViewController.m
//  礼物
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "RecommendViewController.h"

@interface RecommendViewController ()

@end

@implementation RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"推荐";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    
    self.recommenView=[[RecommenView alloc]initWithFrame:MAINSCREEN];
    [self.view addSubview:self.recommenView];
    self.currentPage=20;
    self.recommenView.delegate=self;
    
    self.recommenView.recommenCellArray=[NSMutableArray array];
    NSArray *fileArray1 =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath1 = [fileArray1 lastObject];
    NSString *archivePath1 =[documentPath1 stringByAppendingPathComponent:@"recommen"];
    NSMutableDictionary *data = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath1];
    NSMutableDictionary *Dic=data;
    NSMutableDictionary *Dic1=[Dic objectForKey:@"data"];
    NSMutableArray *array= [Dic1 objectForKey:@"items"];
    for (NSMutableDictionary *dic in array)
    {
        NSMutableDictionary *dic1=[dic objectForKey:@"data"];
        RecommenModel *model=[[RecommenModel alloc]init];
        [model setValuesForKeysWithDictionary:dic1];
        model.description1=[dic1 objectForKey:@"description"];
        model.image_urls=[dic1 objectForKey:@"image_urls"];
        [self.recommenView.recommenCellArray addObject:model];
    }
    [self.recommenView.myRecommenCollectionView reloadData];
    
    [self addHeaderRefresh];
    [self addFooterRefresh];

}
//设置下拉刷新
-(void)addHeaderRefresh
{
    self.recommenView.myRecommenCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    
        //写重新请求数据逻辑（重新AFN请求数据）
        [AppToolss getAFNetWorkingData:@"http://api.liwushuo.com/v2/items?gender=1&generation=2&limit=20&offset=0"
                             andBlock:^(id data){
                                 self.recommenView.recommenCellArray=[NSMutableArray array];
                                 NSFileManager *fileManager=[NSFileManager defaultManager];
                                 NSArray *fileArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                                 NSString *documentPath=[fileArray lastObject];
                                 NSString *archivePath =[documentPath stringByAppendingPathComponent:@"recommen"];
                                 BOOL relust1 =    [fileManager removeItemAtPath:archivePath error:nil];
                                 NSLog(@"==== %@",archivePath);
                                 if (relust1) {
                                     NSLog(@"沙盒文件删除成功");
                                 }else
                                 {
                                     NSLog(@"沙盒文件删除失败");
                                 }
                                 BOOL relust4 =  [NSKeyedArchiver archiveRootObject:data toFile:archivePath];
                                 if (relust4) {
                                     NSLog(@"归档成功");
                                 }else
                                 {
                                     NSLog(@"归档失败");
                                 }
                                 NSMutableDictionary *Dic=data;
                                 NSMutableDictionary *Dic1=[Dic objectForKey:@"data"];
                                 NSMutableArray *array= [Dic1 objectForKey:@"items"];
                                 //     NSLog(@"====%@",array);
                                 for (NSMutableDictionary *dic in array)
                                 {
                                     NSMutableDictionary *dic1=[dic objectForKey:@"data"];
                                     RecommenModel *model=[[RecommenModel alloc]init];
                                     [model setValuesForKeysWithDictionary:dic1];
                                     model.description1=[dic1 objectForKey:@"description"];
                                     model.image_urls=[dic1 objectForKey:@"image_urls"];
                                     [self.recommenView.recommenCellArray addObject:model];
                                 }
                                 [self.recommenView.myRecommenCollectionView reloadData];
                                 [self.recommenView.myRecommenCollectionView.header endRefreshing];
                             } andError:^(id failure) {
                                 [self.recommenView.myRecommenCollectionView.header endRefreshing];
                                 UIAlertView *aView=[[UIAlertView alloc]initWithTitle:@"刷新失败" message:@"请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                 [aView show];
                                 
                             }];
    
}];
    [self.recommenView.myRecommenCollectionView.header beginRefreshing];
    
}

-(void)addFooterRefresh
{
    //当上拉时候会触发该方法
    self.recommenView.myRecommenCollectionView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
         self.currentPage+=20;
        [AppToolss getAFNetWorkingData:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/items?gender=1&generation=2&limit=%ld&offset=0",self.currentPage]
                              andBlock:^(id data){
                                  self.recommenView.recommenCellArray=[NSMutableArray array];
                                  NSFileManager *fileManager=[NSFileManager defaultManager];
                                  NSArray *fileArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                                  NSString *documentPath=[fileArray lastObject];
                                  NSString *archivePath =[documentPath stringByAppendingPathComponent:@"recommen"];
                                  BOOL relust1 =    [fileManager removeItemAtPath:archivePath error:nil];
                                  NSLog(@"==== %@",archivePath);
                                  if (relust1) {
                                      NSLog(@"沙盒文件删除成功");
                                  }else
                                  {
                                      NSLog(@"沙盒文件删除失败");
                                  }
                                  BOOL relust4 =  [NSKeyedArchiver archiveRootObject:data toFile:archivePath];
                                  if (relust4) {
                                      NSLog(@"归档成功");
                                  }else
                                  {
                                      NSLog(@"归档失败");
                                  }
                                  NSMutableDictionary *Dic=data;
                                  NSMutableDictionary *Dic1=[Dic objectForKey:@"data"];
                                  NSMutableArray *array= [Dic1 objectForKey:@"items"];
                                  for (NSMutableDictionary *dic in array)
                                  {
                                      NSMutableDictionary *dic1=[dic objectForKey:@"data"];
                                      RecommenModel *model=[[RecommenModel alloc]init];
                                      [model setValuesForKeysWithDictionary:dic1];
                                      model.description1=[dic1 objectForKey:@"description"];
                                      model.image_urls=[dic1 objectForKey:@"image_urls"];
                                      [self.recommenView.recommenCellArray addObject:model];
                                  }
                                  [self.recommenView.myRecommenCollectionView reloadData];
                                  [self.recommenView.myRecommenCollectionView.footer endRefreshing];
                              } andError:^(id failure) {
                                  [self.recommenView.myRecommenCollectionView.footer endRefreshing];
                              }];
    }];
}







-(void)pushDetailViewController:(RecommenModel *)model
{
   RecommenDetailViewController  *recommenDetailVc=[[RecommenDetailViewController alloc]init];
    recommenDetailVc.model=model;
    recommenDetailVc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:recommenDetailVc animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
